<template>
  <div class="h-full px-4 py-3 rounded-lg bg-[#fff]">
    <div class="flex justify-between">
      <a-button class="ghost" @click="openModal('add')">添加算法</a-button>
      <div class="flex justify-end">
        <a-input
          v-model:value.trim="searchForm.instanceName"
          placeholder="请输入算法名称"
          v-filter-special-chars
          maxLength="50"
          class="_input mr-5"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.categoryName"
          allow-clear
          placeholder="选择算法类别"
          :options="statusList"
        />
        <SerchButtom @search="getList(true, true)" @reset="reset" />
      </div>
    </div>

    <div class="mt-3 h-[calc(100%-6rem)] overflow-y-auto">
      <a-table
        :dataSource="dataSource"
        :columns="columns"
        :pagination="false"
        :loading="loading"
        :row-key="rowKey"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'operation'">
            <div class="flex justify-start" v-if="record.order">
              <a-button
                v-if="record.userName !== 'admin'"
                type="link"
                class="delete"
                @click="openModal('delete', record)"
              >
                删除
              </a-button>
              <a-button
                v-if="record.userName !== 'admin'"
                type="link"
                @click="openModal('edit', record)"
              >
                修改
              </a-button>
            </div>
            <div v-else></div>
          </template>
        </template>
      </a-table>
    </div>

    <div class="mt-3 h-8">
      <Pagination @getListByPage="getListByPage" ref="paginationRef" />
    </div>
  </div>

  <IndexModal
    ref="indexModalRef"
    :selectList="statusList"
    @isOk="getList(true, true)"
  />
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { getDict } from '@/api/dict'

import IndexModal from './IndexModal.vue'
import SerchButtom from '@/components/YuanQi-SearchButtom.vue'

import { list } from './serve.api'

const dataSource = ref()
const loading = ref<boolean>(false)
const paginationRef = ref()
const statusList = ref<any[]>([])

let searchFormClone = {
  pageNum: 1,
  pageSize: 10,
  instanceName: '',
  categoryName: null
}

// 搜索参数
const searchForm = reactive({
  instanceName: '',
  categoryName: null
})

const columns = ref([
  {
    title: '序号',
    dataIndex: 'order',
    width: '7.5rem'
  },
  {
    title: '算法类别',
    dataIndex: 'categoryName',
    key: 'categoryName',
    ellipsis: true
  },
  {
    title: 'API地址',
    dataIndex: 'apiAddress',
    key: 'apiAddress',
    ellipsis: true
  },
  {
    title: '算法名称',
    dataIndex: 'instanceDesc',
    key: 'instanceDesc',
    ellipsis: true
  },
  {
    title: '操作',
    dataIndex: 'operation',
    key: 'operation',
    ellipsis: true,
    width: '12rem'
  }
])

// 重置
const reset = () => {
  searchForm.instanceName = ''
  searchForm.categoryName = null
  getList(true, true)
}

const pagination = reactive({
  total: 0,
  pageSize: 10, // 每页中显示10条数据
  position: ['bottomCenter'],
  current: 1,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条`
})

// 初始化数据
const getList = (flag = false, isSearch = false) => {
  if (flag) {
    pagination.current = 1
    paginationRef.value.changeCurrent(pagination.current)
  }
  loading.value = true
  dataSource.value = []

  const params = {
    instanceName: searchForm.instanceName,
    categoryName: searchForm.categoryName,
    pageNum: pagination.current,
    pageSize: pagination.pageSize
  }
  if (isSearch) {
    searchFormClone = JSON.parse(JSON.stringify(params))
  } else {
    params.instanceName = searchFormClone.instanceName
    params.categoryName = searchFormClone.categoryName
  }

  list(params)
    .then((res) => {
      if (res) {
        console.log(res, '算法列表')

        dataSource.value = res.list
        pagination.total = res.total

        res.list.forEach((i: any, index: any) => {
          i.order = (pagination.current - 1) * pagination.pageSize + index + 1
          i.children = i.algCategoryInstanceVoList
        })

        if (res.list.length === 0 && pagination.current !== 1) {
          pagination.current--
          paginationRef.value.changeCurrent(pagination.current)
          getList(false)
        }

        paginationRef.value.changeTotal(pagination.total)
        loading.value = false
      } else {
        loading.value = false
      }
    })
    .catch(() => {
      loading.value = false
    })
}

const getListByPage = (page: number, pageSize: number) => {
  if (pagination.current !== page) {
    pagination.current = page
    getList()
  }
  if (pagination.pageSize !== pageSize) {
    pagination.pageSize = pageSize
    getList(true)
  }
}

const rowKey = (record: any) => record.id

// 新增/编辑/删除操作
type ModalType = 'add' | 'edit' | 'delete'
const indexModalRef = ref()

const openModal = (type: ModalType, data?: any) => {
  indexModalRef.value.showModal(type, data)
}

onMounted(() => {
  getDict('alg_type').then((res) => {
    statusList.value = res.map((item: any) => ({
      label: item.dictLabel,
      value: item.dictLabel
    }))
  })

  getList(true, true)
})
</script>
<style lang="less" scoped>
._input {
  width: 13.125rem;
  vertical-align: middle;
}
</style>
